package view.editors;

import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.part.EditorPart;
import singleton.Singleton;
import swtElements.Form;
import swtElements.MainSection;
import swtElements.MyBrowser;
import swtElements.MyComposite;
import swtElements.MyLabel;
import swtElements.MySection;
import swtElements.MyText;
import view.editorActions.SaveComments;
import controller.data.Comment;

/**
 * The CommentEditor class is a EditorPart
 * @author kristijanmadunic
 *
 */
public class CommentEditor extends EditorPart implements  ModifyListener {

	/** Is true, if something changed in the editor */
	protected boolean dirty = false;

	/** There are two tab's: editor as form and preview */
	private TabFolder tabfolder; 
	
	/** The editor in the form */
	private MyText textCom; 
	
	/** Contains the text in the second tab (preview) */
	private MyBrowser browserPreview; 
	
	/** Textfield for the personal name */
	private MyText textFirstName; 
	
	/** Textfield for the family name */
	private MyText textLastName;

	/**
	 * When you want to save the document, the method is called
	 */
	@Override
	public void doSave(IProgressMonitor monitor) {
		setDirty(false);
		SaveComments.saveComments(textFirstName, textLastName, textCom);
		setItAgain();
	}

	/**
	 * This Method close the editor and open it again
	 */
	private void setItAgain() {
		IEditorPart thisEditor = getSite().getPage().getActiveEditor();
		getSite().getPage().closeEditor(thisEditor, true);
		setEditorBack(thisEditor);
	}

	/**
	 * This method activate and show the current editor again.
	 * It's called from setItAgain()
	 * @param thisEditor
	 * 			is the current editor (CommenttEditor), which is open
	 */
	private void setEditorBack(IEditorPart thisEditor) {
		try {
			getSite().getWorkbenchWindow().getActivePage().openEditor(
					thisEditor.getEditorInput(),
					thisEditor.getEditorSite().getId());
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void doSaveAs() {
		doSave(null);
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
	}
	
	/**
	 * This method returns the boolean dirty.
	 * Dirty is true, if something changed in the form
	 */
	@Override
	public boolean isDirty() {
		return dirty;
	}

	/**
	 * This method sets the dirty value
	 * @param value
	 * 			is true or false (dirty)
	 */
	protected void setDirty(boolean value) {
		dirty = value;
		firePropertyChange(PROP_DIRTY);
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	/**
	 * It's the main method of the class that creates all swt elements 
	 * in the editor
	 */
	@Override
	public void createPartControl(Composite parent) {

		// Tabfolder

		tabfolder = new TabFolder(parent, SWT.BOTTOM);

		TabItem tabEdit = new TabItem(tabfolder, SWT.LEFT_TO_RIGHT);
		tabEdit.setText("Editor");

		TabItem tabPreview = new TabItem(tabfolder, SWT.LEFT_TO_RIGHT);
		tabPreview.setText("Preview");

		// Basic elements

		FormToolkit toolkitEdit = new FormToolkit(tabfolder.getDisplay());
		Form formEdit = new Form(toolkitEdit, tabfolder); 

		// Info

		MainSection sectionInfo = new MainSection(toolkitEdit, formEdit); 
		sectionInfo.setText(Singleton.getInstance().getCommentText());

		// Author
		
		MySection sectionAuthor = new MySection(toolkitEdit, formEdit, "Author", false, "", false);
		
		MyComposite ClientAuthor = new MyComposite(toolkitEdit, sectionAuthor, 4, false);
		
		new MyLabel(ClientAuthor, "First Name: ", false);

		textFirstName = new MyText(ClientAuthor, 235, false);
		
		new MyLabel(ClientAuthor, "Last Name: ", false);

		textLastName = new MyText(ClientAuthor, 235, false);

		sectionAuthor.setClient(ClientAuthor);

		// Editor
		
		MySection sectionCom = new MySection(toolkitEdit, formEdit, "comment", false, "", false);

		MyComposite ClientCom = new MyComposite(toolkitEdit, sectionCom, 2, false);
		
		new MyLabel(ClientCom, "*", true);
		
		textCom = new MyText(ClientCom, 150, true);
		
		MySection sectionOld = new MySection(toolkitEdit, formEdit, "previous comments", false, "", false);

		MyComposite ClientOldComments = new MyComposite(toolkitEdit, sectionOld, 1, false);
		
		String output = getOutput();
		
		new MyBrowser(ClientOldComments, output, 200, false);

		sectionOld.setClient(ClientOldComments);

		// Add the listeners
		textCom.addModifyListener(this);
		textFirstName.addModifyListener(this);
		textLastName.addModifyListener(this);

		sectionCom.setClient(ClientCom);

		tabEdit.setControl(formEdit.getForm());

		// Preview

		FormToolkit toolkitPreview = new FormToolkit(tabfolder.getDisplay());
		
		Form formPreview = new Form(toolkitPreview, tabfolder);
		
		MySection sectionPreview = new MySection(toolkitPreview, formPreview, "Preview", false, "", true);

		MyComposite ClientPreview = new MyComposite(toolkitPreview, sectionPreview, 1, false);

		browserPreview = new MyBrowser(ClientPreview, Singleton.getInstance().getComment(), 0, true);

		sectionPreview.setClient(ClientPreview);

		tabPreview.setControl(formPreview.getForm());
	}
	
	/**
	 * Creates of a list of comments a string
	 * @return output
	 * 			returns a string of the previous comments
	 */
	private String getOutput(){
		String output = "";
		List<Comment> list = Singleton.getInstance().getCommentPrevious();
		for (int i = 0; i < list.size(); i++) {
			Comment tmp = list.get(i);
			output += "<hr><b>";
			String tmpName = tmp.getFirstname();
			String tmpLastName = tmp.getLastname();
			if (tmpName.trim().compareTo("") == 0 && tmpLastName.trim().compareTo("") == 0)
				output += "NoName:";
			else if (tmpLastName.trim().compareTo("") == 0)
				output += tmpName+":";
			else if(tmpName.trim().compareTo("") == 0)
				output += tmpLastName+":";
			else if(tmpName.trim().compareTo("") != 0 && tmpLastName.trim().compareTo("") != 0)
				output += tmpName + " "+tmpLastName;
			
			output += "</b>"+" ["+tmp.getDate()+"]"+"<br>";
			output += tmp.getContent();
		}
		return output;
	}

	@Override
	public void setFocus() {
		tabfolder.setFocus();
	}

	/**
	 * It's a listener for the text areas, so the active preview is correct
	 */
	public void modifyText(ModifyEvent e) {
		// remeove the listeners
		textCom.removeModifyListener(this);
		textFirstName.removeModifyListener(this);
		textLastName.removeModifyListener(this);

		String test = "";
		String output = textCom.getText();
		if (textFirstName.getText().length() > 0
				&& textLastName.getText().length() > 0) {
			test = "<hr><b>" + textFirstName.getText() + " "
					+ textLastName.getText() + ":<br>" + "</b> " + output
					+ "<hr>";
		} else if (textFirstName.getText().length() > 0) {
			test = "<hr><b>" + textFirstName.getText() + ":<br>" + "</b> "
					+ output + "<hr>";
		} else if (textLastName.getText().length() > 0) {
			test = "<hr><b>" + textLastName.getText() + ":<br>" + "</b> "
					+ output + "<hr>";
		} else if (textFirstName.getText().length() == 0
				&& textLastName.getText().length() == 0) {
			test = "<hr><b>" + "No Name" + ":<br>" + "</b> "+ output + "<hr>";
		}
		browserPreview.setText(test);

		// Save, if everything is filled, so first name, last name and doc!
		if (textCom.getText().length() > 0) {
			setDirty(true);
		} else {
			setDirty(false);
		}

		// Add the listeners back
		textCom.addModifyListener(this);
		textFirstName.addModifyListener(this);
		textLastName.addModifyListener(this);
	}
}
